#include "head.h"

char* removeTrailingNewline(char* str) {
    int len = strlen(str);
    if (len > 0 && (str[len-1] == '\n' || str[len-1] == '\r')) {
        str[len-1] = '\0'; // 如果最后一个字符是'\n'或'\r'，将其替换为字符串终止符
    }
    return str;
}


int analyze_cmd(int net_fd, char *str, request_t * request,thread_status *pthread_info){

        // 清除后面的空格
        char * arg = removeTrailingNewline(str);
        char * cmd = strtok(arg," ");
        //以空格符作为分隔        
        if(cmd == NULL){
            //printf("Error :Command missing\n");
            return 1;
        }
        //获取下一个参数
        char * args = strtok(NULL," ");
      
        int ret;
        if(strcmp(cmd,"cd")==0){
            //printf("flag = CD\n");
            request->flag = CD;
            ret = net_cd(net_fd,args,request);
        }else if(strcmp(cmd,"ls")==0){
            //printf("flag = LS\n");
            request->flag = LS;
            ret = net_ls(net_fd, request);
        }else if(strcmp(cmd,"puts")==0){
            //printf("flag = PUTS\n");
            //创建子线程，进行长命令（文件传输）操作
            //子线程的参数要放到struct结构体中：子线程id，运行状态，socket_fd,mutex,请求             
            if(args == NULL || strlen(args) == 0){
                return 1;
            }
            request->flag = PUTS;
            strcpy(request->args, args);
            // 创建临时请求报文给子线程           
            request_t *temp_request = (request_t*)calloc(1,sizeof(request_t));
            memcpy(temp_request, request, sizeof(request_t));
            pthread_info->request = temp_request;
            //子线程的参数要放到struct结构体中：子线程id，运行状态，socket_fd,mutex,请求
            pthread_mutex_lock(&pthread_info->lock);
            for(int i=0; i<pthread_info->length; i++){
                if(pthread_info->status[i] == 0){
                    pthread_info->status[i] = 1;
                    pthread_create(&pthread_info->thread_ids[i], NULL, threadMain, (void*)pthread_info);
                    break;
                }
            } 
            pthread_mutex_unlock(&pthread_info->lock);   
            return 0;
        }else if(strcmp(cmd,"gets")==0){
            // printf("flag = GETS\n");
            // 创建子线程，进行长命令（文件传输）操作
            // 子线程的参数要放到struct结构体中：子线程id，运行状态，socket_fd,mutex,请求
            //malloc(sizeof(request->args));
            if(args == NULL || strlen(args) == 0){
                return 1;
            }
            request->flag = GETS;
            strcpy(request->args, args);
            // 创建临时请求报文给子线程
            request_t *temp_request = (request_t*)calloc(1,sizeof(request_t));
            memcpy(temp_request, request, sizeof(request_t));
            pthread_info->request = temp_request;
            //子线程的参数要放到struct结构体中：子线程id，运行状态，socket_fd,mutex,请求
            pthread_mutex_lock(&pthread_info->lock);
            for(int i=0; i<pthread_info->length; i++){
                if(pthread_info->status[i] == 0){
                    pthread_info->status[i] = 1;
                    pthread_create(&pthread_info->thread_ids[i], NULL, threadMain, (void*)pthread_info);
                    break;
                }
            } 
            pthread_mutex_unlock(&pthread_info->lock);   
            return 0;
        }else if(strcmp(cmd,"rm")==0){
            //printf("flag = RM\n");
            request->flag = RM;
            ret = net_rm(net_fd, args, request);
        }else if(strcmp(cmd,"pwd")==0){
            //printf("flag = PWD\n");
            request->flag = PWD;
            ret = net_pwd(net_fd,request);
        }else if(strcmp(cmd,"mkdir")==0){
            //printf("flag = MKDIR\n");
            request->flag = MKDIR;
            ret = net_mkdir(net_fd, args,  request);
        }else if(strcmp(cmd,"exit")==0){
            memset(request->args, 0, sizeof(request->args));
            request->flag = EXIT;
            printf("%s\n", request->token);
            send(net_fd, request, sizeof(request_t), 0);
            return 2; // 正常退出
        }else{
            //printf("aaa\n");
            return 1; // 未识别的命令，返回错误
        }

        if(ret == 3){
            // 重连失败
            return 3;
        }
        if(ret == -1){
            return -1;
        }
    return 0;
}



